package tq.com.tqteacher.discuss.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.browser.DetailPicsActivity;

import java.util.ArrayList;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqteacher.activity.playerView.TQPlayerActivity;
import tq.com.tqteacher.discuss.factory.TQDiscussFactory;
import tq.com.tqteacher.discuss.vh.IDiscussItemClickListener;
import tq.com.tqteacher.discuss.vh.TQDiscussViewHold;
import tq.com.tqteacher.discuss.vh.TQLeftViewHold;
import tq.com.tqteacher.discuss.vh.TQRightViewHold;
import tq.com.tqteacher.model.TQCommentOverInfoModel;
import tq.com.tqteacher.net.TQNetInterface;

public class TQMsgAdapter extends RecyclerView.Adapter implements IDiscussItemClickListener {
    protected Context mContext;
    protected ArrayList<TQCommentOverInfoModel.TQCommentInfoModel> mCommentInfoModels = new ArrayList<>();

    public TQMsgAdapter(Context pContext){
        mContext = pContext;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, int pI) {
        return TQDiscussFactory.getInstance(pI, mContext, pViewGroup);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        TQCommentOverInfoModel.TQCommentInfoModel lCommentInfoModel = mCommentInfoModels.get(pI);

        ((TQDiscussViewHold)pViewHolder).setIDiscussItemClickListener(this);
        if(pViewHolder instanceof TQLeftViewHold){
            ((TQLeftViewHold)pViewHolder).bindData(lCommentInfoModel);
        }else if(pViewHolder instanceof TQRightViewHold){
            ((TQRightViewHold)pViewHolder).bindData(lCommentInfoModel);
        }
    }

    @Override
    public int getItemCount() {
        return mCommentInfoModels.size();
    }

    @Override
    public int getItemViewType(int position) {
        TQCommentOverInfoModel.TQCommentInfoModel lCommentInfoModel = mCommentInfoModels.get(position);
        return TQDiscussFactory.getDirectory(lCommentInfoModel);
    }

    public void appendNew(TQCommentOverInfoModel.TQCommentInfoModel pInfoModel){
        if(pInfoModel == null) return;

        int pos = mCommentInfoModels.size();
        mCommentInfoModels.add( pInfoModel);
        notifyItemInserted(pos);
    }

    public void appendMore(ArrayList<TQCommentOverInfoModel.TQCommentInfoModel> pTQCommentInfoModels){
        if(pTQCommentInfoModels == null);

        mCommentInfoModels.addAll(0, pTQCommentInfoModels);
        notifyDataSetChanged();
    }

    public void deleteOnew(TQCommentOverInfoModel.TQCommentInfoModel pInfoModel){
        int pos = mCommentInfoModels.indexOf(pInfoModel);
        mCommentInfoModels.remove(pInfoModel);
        TQEventBus.post(new TQEventBus.TQEventMessage(TQEventBus.TQEventMessage.Opertor.delete, pInfoModel));
        notifyItemRemoved(pos);
    }

    @Override
    public void openPic(TQCommentOverInfoModel.TQCommentInfoModel pInfoModel) {
        if(pInfoModel == null || pInfoModel.getType() != TQSystemDefine.LeaveMsgType_image) return;

        String url = TQPictureUpload.downloadPath(pInfoModel.getContent());
        ArrayList<String> pics = new ArrayList<>();
        pics.add(url);

        DetailPicsActivity.jumpToThis((Activity)mContext, pics, 0);
    }

    @Override
    public void openVideo(TQCommentOverInfoModel.TQCommentInfoModel pInfoModel) {
        if(pInfoModel == null || pInfoModel.getType() != TQSystemDefine.LeaveMsgType_video) return;

        String url = TQPictureUpload.downloadPath(pInfoModel.getContent());
        TQPlayerActivity.showPlayer(url, (Activity)mContext);
    }

    public void deleteItem(final TQCommentOverInfoModel.TQCommentInfoModel pInfoModel){
        if(pInfoModel == null)return;

        ArrayList<Integer> ids = new ArrayList<>();
        ids.add(pInfoModel.getId());


        TQNetInterface.delComm(ids, new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                if(!pNetResult.isSuccess()){
                    AlertDialogHelper.show(mContext, pNetResult.getErrMsg());
                    return;
                }
                deleteOnew(pInfoModel);
            }
        });
    }
}
